# mqtt_local.py Local configuration for mqtt_as demo programs.
from sys import platform, implementation
from mqtt_as import config

# ---------- 必须修改7个全局定义数据（产品ID、用户ID、服务器地址、用户名、密码、WIFI账号、WIFI密码） ----------

PRODUCT_ID = 'XXX'                       # 1、产品ID（填入你的产品ID）
USER_ID = 'XXX'         # 2、用户ID（填入你的用户ID）

config["server"] = "XXX"    # 3、服务器地址（填入你的MQTT服务器）
config["user"] = "XXX"              # 4、用户名（填入你的MQTT用户名）
config["password"] = "XXX" # 5、密码（填入你的MQTT密码）


config["ssid"] = "XXX"                # 6、WIFI 账号
config["wifi_pw"] = "XXX"        # 7、WIFI密码


# ---------- 定义4个引脚用于控制四个外设 ----------
AC_PIN = 15    # 空调引脚
ALARM_PIN = 16 # 告警引脚
FAN_PIN = 17   # 风扇引脚
LAMP_PIN = 18  # 台灯引脚






# 定义引脚用于控制四个外设 - 使用ESP32有效GPIO引脚
try:
    if platform == "esp8266" or platform == "esp32":
        from machine import Pin
        # 配置为输出模式以便控制设备
        LAMP_PIN = Pin(LAMP_PIN, Pin.OUT)  # 台灯引脚
        AC_PIN = Pin(AC_PIN, Pin.OUT)    # 空调引脚
        ALARM_PIN = Pin(ALARM_PIN, Pin.OUT) # 告警引脚
        FAN_PIN = Pin(FAN_PIN, Pin.OUT)   # 风扇引脚
        
        # 初始状态设为关闭（高电平表示打开，低电平表示关闭）
        LAMP_PIN.value(0)  # 初始为低电平，表示关闭
        AC_PIN.value(0)    # 初始为低电平，表示关闭
        ALARM_PIN.value(0) # 初始为低电平，表示关闭
        FAN_PIN.value(0)   # 初始为低电平，表示关闭
        
        # 初始化I2C用于SHT31传感器 - 使用ESP32常用I2C引脚
        # i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=100000)
        # SHT31_ADDR = 0x44  # SHT31默认I2C地址
except Exception as e:
    print(f"硬件初始化失败: {e}")



config['ssl'] = False               # 非SSL连接（端口1883）
config["port"] = 1883                # 设备认证信息


# 连接稳定性配置
config["keepalive"] = 60            # 心跳间隔60秒
config["max_reconnects"] = 5        # 最大重试次数（超过后报错）
config["reconnect_delay"] = 3       # 重试间隔2秒（递增）



# For demos ensure same calling convention for LED's on all platforms.
# ESP8266 Feather Huzzah reference board has active low LED's on pins 0 and 2.
# ESP32 is assumed to have user supplied active low LED's on same pins.
# Call with blue_led(True) to light

if platform == "esp8266" or platform == "esp32":
    from machine import Pin

    def ledfunc(pin, active=0):
        pin = pin

        def func(v):
            pin(not v)  # Active low on ESP8266

        return pin if active else func

    wifi_led = ledfunc(Pin(0, Pin.OUT, value=0))  # Red LED for WiFi fail/not ready yet
    blue_led = ledfunc(Pin(2, Pin.OUT, value=1))  # Message received
    # Example of active high LED on UM Feather S3
    # blue_led = ledfunc(Pin(13, Pin.OUT, value = 0), 1)  # Message received ESP32-S3
elif platform == "pyboard":
    from pyb import LED

    def ledfunc(led, init):
        led = led
        led.on() if init else led.off()

        def func(v):
            led.on() if v else led.off()

        return func

    wifi_led = ledfunc(LED(1), 1)
    blue_led = ledfunc(LED(3), 0)
elif (platform == "rp2") and ("Pico" in implementation._machine):
    from machine import Pin

    def ledfunc(pin):
        pin = pin

        def func(v):
            pin(v)

        return func

    wifi_led = lambda _: None  # Only one LED
    LED = "LED" if "W" in implementation._machine else 25
    blue_led = ledfunc(Pin(LED, Pin.OUT, value=0))  # Message received
else:  # Assume no LEDs
    wifi_led = lambda _: None
    blue_led = wifi_led
